﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.DALFactory;

    public class RateDetailBLL : IBaseInfoDetail
    {
        private static readonly IBillingEntityDA BillingEntityDA = DataAccess.CreateBillingEntityDA();
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private PubParasInfo pubParasInfo;
        private static readonly IRateDetailDA rateDetailDA = DataAccess.CreateRateDetailDA();

        public RateDetailBLL()
        {
            this.pubParasInfo = null;
        }

        public RateDetailBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public RateDetailBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string DeleteDetail(List<string> mainID, List<string> subId, List<string> pk3)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string temp = string.Empty;
            try
            {
                List<RateDetailInfo> RateInfos = new List<RateDetailInfo>();
                RateDetailInfo subjectInfo = null;
                foreach (string id in subId)
                {
                    subjectInfo = rateDetailDA.GetRateDetailByID(dataBase, tran, mainID[0], id, this.dataBaseID[0]);
                    RateInfos.Add(subjectInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (rateDetailDA.DeleteRateDetail(dataBase, tran, mainID[0], subId, this.dataBaseID[0]) > 0)
                {
                    for (int i = 0; i < subId.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<RateDetailInfo>(dataBase, tran, RateInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "RATE", this.dataBaseID[0]);
                        temp = temp + subId[i] + ",";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllDetailInfo()
        {
            return rateDetailDA.GetAllRateDetail(this.dataBaseID[0]);
        }

        public DataSet GetDetaiInfolByQueryEntity(object detailQuery)
        {
            throw new NotImplementedException();
        }

        public object GetDetailInfoByID(string mainID, string subID, string pk3)
        {
            return rateDetailDA.GetRateDetailByID(mainID, subID, this.dataBaseID[0]);
        }

        public List<object> GetDetailInfoListByID(string mainID, string subID)
        {
            throw new NotImplementedException();
        }

        public string InsertDetail(object obj)
        {
            RateDetailInfo rateDetailInfo = obj as RateDetailInfo;
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string strContractID = string.Empty;
            try
            {
                if (!string.IsNullOrEmpty(rateDetailInfo.BillingEntity) && !BillingEntityDA.CheckBillingEntityIDUnique(dataBase, tran, rateDetailInfo.BillingEntity, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2150_003", this.pubParasInfo.UserLanguage, new List<string> { rateDetailInfo.BillingEntity });
                }
                rateDetailInfo.UpdatedBy = rateDetailInfo.CreatedBy;
                rateDetailInfo.CreatedDate = DateTime.Now;
                rateDetailInfo.UpdatedDate = DateTime.Now;
                if (rateDetailDA.InsertRateDetail(dataBase, tran, rateDetailInfo, whLoginID) > 0)
                {
                    strContractID = rateDetailInfo.RateID;
                    new AuditDataBLL().InsertAuditData<object>(dataBase, tran, null, obj, AuditAction.Insert, this.pubParasInfo.UserID, "RATE_DETAIL", this.dataBaseID[0]);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return strContractID;
        }

        public string UpdateDetail(object obj)
        {
            RateDetailInfo rateDetailInfo = obj as RateDetailInfo;
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string strContractID = string.Empty;
            try
            {
                if (!string.IsNullOrEmpty(rateDetailInfo.BillingEntity) && !BillingEntityDA.CheckBillingEntityIDUnique(dataBase, tran, rateDetailInfo.BillingEntity, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2150_003", this.pubParasInfo.UserLanguage, new List<string> { rateDetailInfo.BillingEntity });
                }
                RateDetailInfo oldRateInfo = rateDetailDA.GetRateDetailByID(dataBase, tran, rateDetailInfo.RateID, rateDetailInfo.LineID.ToString(), this.dataBaseID[0]);
                if (rateDetailDA.UpdateRateDetail(dataBase, tran, rateDetailInfo, whLoginID) > 0)
                {
                    strContractID = rateDetailInfo.RateID;
                    new AuditDataBLL().InsertAuditData<object>(dataBase, tran, oldRateInfo, obj, AuditAction.Update, this.pubParasInfo.UserID, "RATE_DETAIL", this.dataBaseID[0]);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return strContractID;
        }

        public string UpdateIsActive(List<string> mainID, List<string> subId, string isActive)
        {
            throw new NotImplementedException();
        }
    }
}

